import Uts from "@module/Uts";

export default {
    isNum(str) {
        let reg = /^[0-9]+\.?[0-9]{0,2}$/;
        if (reg.test(str)) {
            return true;
        }
        return false;
    },

    getDateStr(times) {
        const date = new Date(times - 0);
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        return year + "-" + month + "-" + day;
    },

    getMinuteStr(times) {
        const date = new Date(times - 0);
        const year = date.getFullYear()<10?"0"+date.getFullYear().toString():date.getFullYear().toString();
        const month = date.getMonth() + 1<10?"0"+(date.getMonth() + 1).toString():(date.getMonth() + 1).toString();
        const day = date.getDate()<10?"0"+date.getDate().toString():date.getDate().toString();
        const hou = date.getHours()<10?"0"+date.getHours().toString():date.getHours().toString();
        const min = date.getMinutes()<10?"0"+date.getMinutes().toString():date.getMinutes().toString();
        return year + "-" + month + "-" + day + " " + hou + ":" + min;
    },

    parseUrl(url) {
    },

    parseDetailUrl() {
        let hashUrl = window.location.hash;
        let patt = /reactDetail\/([0-9]+)\/([0-9]+)$/;
        let params = hashUrl.match(patt);
        let [, productId, suppId] = params;
        return {
            productId,
            suppId
        };
    },

    parseSearchlUrl() {
        let hashUrl = window.location.hash;
        let paramsStr = hashUrl.split("?")[1];
        let paramsArr = paramsStr.split("&");

        let paramsObj = {};
        paramsArr.forEach(item => {
            let key = item.split("=")[0];
            let value = item.split("=")[1];
            Object.assign(paramsObj, {[key]: decodeURIComponent(value)});
        });
        return paramsObj;
    },

    paserSuppUrl() {
        let hashUrl = window.location.hash;
        let patt = /searchResults\/([\s\S]*)$/;
        let params = hashUrl.match(patt);
        let [, supp] = params;
        return decodeURIComponent(supp);
    },

    paserStatementUrl() {
        let hashUrl = window.location.hash;
        let paramsStr = hashUrl.split("?")[1];
        let key = paramsStr.split("=")[0];
        let value = paramsStr.split("=")[1];
        return {
            [key]: value
        };
    },

    paserSupplierShopUrl() {
        let hashUrl = window.location.hash;
        let paramsStr = hashUrl.split("?")[1];
        let key = paramsStr.split("=")[0];
        let value = paramsStr.split("=")[1];
        return {
            [key]: value
        };
    },

    // 过滤后台返回的span标签
    filterSpan(txt) {
        if (txt.indexOf(`<span`) == -1) {
            return txt;
        } else {
            let patt = /([\s\S]*)\<span/;
            let strArr = txt.match(patt);
            return strArr[1];
        }
    },

    scrollToTop() {
        const doc = document.documentElement.scrollTop
            ? document.documentElement
            : document.body;
        doc.scrollTop = 0;
    },

    getElementTop(element) {
        let actualTop = element.offsetTop;
        let current = element.offsetParent;
        while (current !== null) {
            actualTop += current.offsetTop;
            current = current.offsetParent;
        }
        return actualTop;
    },

    scrollToById(id) {
        const ele = document.querySelector(`#${id}`);
        let offsetTop = this.getElementTop(ele);
        let doc = document.documentElement.scrollTop
            ? document.documentElement
            : document.body;
        doc = !this.isIe() ? document.documentElement : document.body;

        doc.scrollTop = offsetTop;
    },

    isIe() {
        if (this.isEdge()) {
            return true;
        }
        if (navigator.userAgent.indexOf("MSIE") > 0) {
            return true;
        } else {
            return false;
        }
    },

    isEdge() {
        if (navigator.userAgent.indexOf("Edge") > -1) {
            return true;
        }
        return false;
    },

    addFavorite(obj, opts) {
        var _t, _u;
        if (typeof opts != "object") {
            _t = document.title;
            _u = location.href;
        } else {
            _t = opts.title || document.title;
            _u = opts.url || location.href;
        }
        try {
            window.external.addFavorite(_u, _t);
        } catch (e) {
            if (window.sidebar) {
                obj.href = _u;
                obj.title = _t;
                obj.rel = "sidebar";
            } else {
                alert(
                    "抱歉，您所使用的浏览器无法完成此操作。\n\n请使用 Ctrl + D 将本页加入收藏夹！"
                );
            }
        }
    },


    getRandomArray(arr, count) {
        var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
        while (i-- > min) {
            index = Math.floor((i + 1) * Math.random());
            temp = shuffled[index];
            shuffled[index] = shuffled[i];
            shuffled[i] = temp;
        }
        return shuffled.slice(min);
    },


    getTwoDimenArr(arr,count){
        let newArr = [];
        let  len = arr.length;
        for(let i = 0, j = 0; i < len; i += count, j++){
            newArr[j] = arr.splice(0, count);
        }
        return newArr
    },


    getCutLength(value,defaultLen){
        if(value){
            let letterLen=this.getEnglishLetterLen(value);
            let len=defaultLen+Math.floor(letterLen/2);
            return  len;
        }
        return defaultLen;
    },

    getEnglishLetterLen(val){
        if(val){
            if(/[a-z\(\)]/.test(val)){
                return val.match(/[a-z\(\)]/g).length;
            }
        }
        return 0;
    }







}

